package logic

import (
	"context"
	"errors"
	"short-video-manager/models/mysql"
	"strings"

	"short-video-manager/videorpc/internal/svc"
	"short-video-manager/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type CommentAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCommentAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommentAddLogic {
	return &CommentAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CommentAddLogic) CommentAdd(in *videorpc.CommentAddRequest) (*videorpc.CommentAddResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.UserInfo

	if err := user.FindUserById(mysql.DB, in.UserId); err != nil {
		return nil, errors.New("未登录请先登录")
	}
	var video mysql.VideoInfo

	if err := video.FindVideoById(mysql.DB, in.VideoId); err != nil {
		return nil, errors.New("视频不存在,评论失败")
	}
	if in.CommentContent == "" {
		return nil, errors.New("评论不能为空")
	}
	if strings.Contains(in.CommentContent, "垃圾") || strings.Contains(in.CommentContent, "骗人") {
		return nil, errors.New("具有敏感词汇，请修改后评论")
	}
	comment := mysql.CommentInfo{
		Model:          gorm.Model{},
		VideoId:        int(in.VideoId),
		UserId:         int(in.UserId),
		CommentContent: in.CommentContent,
		Anonymous:      int(in.Anonymous),
	}

	if err := comment.CommentAdd(mysql.DB); err != nil {
		return nil, errors.New("评论失败")
	}
	return &videorpc.CommentAddResponse{
		CommentId: int64(comment.ID),
	}, nil
}
